package com.inforich.statistics.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.inforich.common.utils.DictUtils;
import com.inforich.common.utils.JsonUtil;
import com.inforich.company.service.ITransportCompanyService;
import com.inforich.disposal.service.IDisposalLandService;
import com.inforich.orderManage.service.IBookingService;
import com.inforich.orderManage.service.ITransportService;
import com.inforich.projectManage.service.IProjectService;
import com.inforich.statistics.domain.StatisticsVo;
import com.inforich.statistics.mapper.StatisticsMapper;
import com.inforich.statistics.service.IStatisticsService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description: some desc
 * @author: HJM
 * @date: 2022/10/31
 */
@Service
public class StatisticsServiceImpl implements IStatisticsService {

  @Autowired
  private IProjectService projectService;
  @Autowired
  private ITransportCompanyService transportCompanyService;
  @Autowired
  private IDisposalLandService disposalLandService;
  @Autowired
  private IBookingService bookingService;
  @Autowired
  private ITransportService transportService;
  @Autowired
  private StatisticsMapper statisticsMapper;

  @Override
  public JSONObject totalData() {
    JSONObject result = new JSONObject();
    int projectNum = projectService.count();
    int companyNum = transportCompanyService.count();
    int disposalLandNum = disposalLandService.count();
    int bookingNum = bookingService.count();
    int transportNum = transportService.count();

    result.put("projectNum", projectNum);
    result.put("companyNum", companyNum);
    result.put("disposalLandNum", disposalLandNum);
    result.put("bookingNum", bookingNum);
    result.put("transportNum", transportNum);
    return result;
  }

  @Override
  public List<StatisticsVo> groupProjectByKind() {
    List<StatisticsVo> list = statisticsMapper.groupProjectByKind();
    list.forEach(l -> {
      l.setName(DictUtils.getDictLabel("project_kind_id", l.getName()));
      l.setValue(String.valueOf(l.getNum()));
    });
    return list;
  }

  @Override
  public JSONObject groupBookingAndTransportByCompany() {
    List<StatisticsVo> bList = statisticsMapper.groupBookingByCompany();
    List<StatisticsVo> tList = statisticsMapper.groupTransportByCompany();

    List<JSONObject> indicator = Lists.newArrayList();
    bList.forEach(b -> {
      indicator.add(JsonUtil.of("name", b.getName()));
    });
    return null;
  }

  @Override
  public JSONObject groupBookingAndTransportByMonth() {
    List<StatisticsVo> bList = statisticsMapper.groupBookingByMonth();
    List<StatisticsVo> tList = statisticsMapper.groupTransportByMonth();

    JSONObject result = new JSONObject();
    // 横坐标月份数据
    List<String> xAxis_data = bList.stream().map(StatisticsVo::getValue)
        .collect(Collectors.toList());
    result.put("xAxis_data", xAxis_data);
    // 预约单数据
    List<Integer> bookingData = bList.stream().map(StatisticsVo::getNum)
        .collect(Collectors.toList());
    result.put("bookingData", bookingData);
    // 运输单数据
    Map<String, Integer> tMap = new HashMap<>();
    tList.forEach(t -> tMap.put(t.getValue(), t.getNum()));
    List<Integer> transportData = xAxis_data.stream().map(d -> {
      Integer num = tMap.get(d);
      return num == null ? 0 : num;
    }).collect(Collectors.toList());
    result.put("transportData", transportData);
    return result;
  }

  @Override
  public JSONObject groupApproachByMonth() {
    List<StatisticsVo> list = statisticsMapper.groupApproachByMonth();

    JSONObject result = new JSONObject();
    // 横坐标月份数据
    List<String> xAxis_data = list.stream().map(StatisticsVo::getValue)
        .collect(Collectors.toList());
    result.put("xAxis_data", xAxis_data);

    List<Integer> yData = list.stream().map(StatisticsVo::getNum).collect(Collectors.toList());
    result.put("yData", yData);

    return result;
  }

  @Override
  public JSONObject groupApproachByDisposal() {
    List<StatisticsVo> list = statisticsMapper.groupApproachByDisposal();

    JSONObject result = new JSONObject();
    result.put("data", list);
    return result;
  }
}
